#include "config_main.h"
#ifdef DDENN_DATAREFS_SAVE
/*
 * Ddenn Plugin by Darvin. any comments to nbdarvin@gmail.com
 * datarefs_save.cpp
 *
 *  Created on: 14.06.2009
 *      Author: Darvin
 */


#include "datarefs_save.h"
/**************************************************************************************/
//
//          it provides datarefs_save
//
/*************************************************************************************/

int DdennDatarefsSave::ddenn_datarefs_save_count;
DdennDatarefsSave::ddenn_datarefs_save_struct DdennDatarefsSave::ddenn_datarefs_save[DDENNDATAREFSSAVE_MAXCOUNT];


void DdennDatarefsSave::SetDatarefFromDatarefSave(const ddenn_datarefs_save_struct ds){
	if (((ds).datatype)==xplmType_Int){
		SetDataRefI((ds).name,(ds).valueint);
	}
	if (((ds).datatype)==xplmType_Float){
		SetDataRefF((ds).name,(ds).valuefloat);
	}
	if (((ds).datatype)==xplmType_Double){
		SetDataRefD((ds).name,(ds).valuedouble);
	}
}

void DdennDatarefsSave::GetDatarefToDatarefSave(ddenn_datarefs_save_struct * ds){

	if (((*ds).datatype)==xplmType_Int){
		(*ds).valueint=GetDataRefI((*ds).name);
	}
	if (((*ds).datatype)==xplmType_Float){
		(*ds).valuefloat=GetDataRefF((*ds).name);
	}
	if (((*ds).datatype)==xplmType_Double){
		(*ds).valuedouble=GetDataRefD((*ds).name);
	}
}




void DdennDatarefsSave::RegisterDatarefsSave(){
	FILE *F;
	char buf[255]="";
	if (F = fopen(
			getFileNameinPluginDir("datarefs_save.cfg")
			,"r")){
		int i;
		//i=i/0;
		for (i=0;
			(fgets(buf,255,F)!=NULL);
			i++){
			char buf2[255];
			sscanf(buf,"%s%s",(ddenn_datarefs_save[i]).name,buf2);


			if (!strcmp (buf2,"int")){
				(ddenn_datarefs_save[i]).datatype=xplmType_Int;
			//	sscanf(buf,"%s %s %d",(ddenn_datarefs_save[i]).name,buf2,&((ddenn_datarefs_save[i]).valueint));
			}
			if (!strcmp (buf2,"float")){
				(ddenn_datarefs_save[i]).datatype=xplmType_Float;
			//	sscanf(buf,"%s %s %f",(ddenn_datarefs_save[i]).name,buf2,&((ddenn_datarefs_save[i]).valuefloat));
			}
			if (!strcmp (buf2,"double")){
				(ddenn_datarefs_save[i]).datatype=xplmType_Double;
			//	sscanf(buf,"%s %s %f",(ddenn_datarefs_save[i]).name,buf2,&((ddenn_datarefs_save[i]).valuedouble));
			}
		}
		ddenn_datarefs_save_count=i;
		fclose(F);
	}
}

void DdennDatarefsSave::UnregisterDatarefsSave(){
	FILE *F;
	if (F = fopen(
			getFileNameinPluginDir("datarefs_save.dat")
			,"w")){
		int i;
		for (i=0;
			(i<=ddenn_datarefs_save_count);
			i++){
			GetDatarefToDatarefSave(&(ddenn_datarefs_save[i]));
			if ((ddenn_datarefs_save[i]).datatype==xplmType_Int){
				char * buf2="int";
				fprintf(F,"%s %s %d\n",(ddenn_datarefs_save[i]).name,buf2,((ddenn_datarefs_save[i]).valueint));
			}
			if ((ddenn_datarefs_save[i]).datatype==xplmType_Float){
				char * buf2="float";
				fprintf(F,"%s %s %f\n",(ddenn_datarefs_save[i]).name,buf2,((ddenn_datarefs_save[i]).valuefloat));
			}
			if ((ddenn_datarefs_save[i]).datatype==xplmType_Double){
				char * buf2="double";
				fprintf(F,"%s %s %f\n",(ddenn_datarefs_save[i]).name,buf2,((ddenn_datarefs_save[i]).valuedouble));
			}
		}
		fclose(F);
	}
}





void DdennDatarefsSave::onLoad(){
	FILE *F;
	char buf[255]="";
	if (F = fopen(
			getFileNameinPluginDir("datarefs_save.dat")
			,"r")){

		for (int i=0;
			(fgets(buf,255,F)!=NULL);){
			char buf2[255];
			char tempname[255];
			sscanf(buf,"%s%s",tempname,buf2);

			if (!strcmp (tempname,(ddenn_datarefs_save[i]).name)){
				if (!strcmp (buf2,"int")){
					(ddenn_datarefs_save[i]).datatype=xplmType_Int;
					sscanf(buf,"%s %s %d",(ddenn_datarefs_save[i]).name,buf2,&((ddenn_datarefs_save[i]).valueint));
				}
				if (!strcmp (buf2,"float")){
					(ddenn_datarefs_save[i]).datatype=xplmType_Float;
					sscanf(buf,"%s %s %f",(ddenn_datarefs_save[i]).name,buf2,&((ddenn_datarefs_save[i]).valuefloat));
				}
				if (!strcmp (buf2,"double")){
					(ddenn_datarefs_save[i]).datatype=xplmType_Double;
					sscanf(buf,"%s %s %lf",(ddenn_datarefs_save[i]).name,buf2,&((ddenn_datarefs_save[i]).valuedouble));
				}
				SetDatarefFromDatarefSave(ddenn_datarefs_save[i]);
				i++;
			}
		}
		fclose(F);
	}
}


/**************************************************************************************/
//
//          end of datarefs_save
//
/*************************************************************************************/

#endif
